﻿// Content is a leaf UI component, it does not have children components
define([
    'Xjs/core/util',
    'Xjs/core/Component',
    'Xjs/ui/HtmlRenderer',
    'Xjs/core/DomManipulator',
    'Xjs/core/ContentBound',
    'Xjs/core/Interactive'
],
function (util, Component, HtmlRenderer, DomManipulator, ContentBound, Interactive) {
    'use strict';

    var type = 'content',
    Content = Xjs.getType(type);

    if (Content) { // It has been already loaded
        return Content;
    }
    // It is the basic content unit
    // The content component can have content and attributes but it cannot have children components.
    // It can be bound to data through a binder the component must declare
    Content = util.define({
        base: Component,
        mixins: [
            HtmlRenderer,
            DomManipulator,
            ContentBound,
            Interactive
        ],
        construct: function (conf, parent) {

            this.requiresId = conf.requiresId || this.requiresId;

            if (conf.binder) { // To bind we need and id
                this.requiresId = true;
            }

            Content.base.constructor.call(this, conf, parent);

            HtmlRenderer.prototype.initialize.call(this, conf);
            DomManipulator.prototype.initialize.call(this, conf);
            ContentBound.prototype.initialize.call(this, conf);
            Interactive.prototype.initialize.call(this, conf);

            this.content = conf.content;
        },
        type: type,
        tag: 'div',

        ensureId: function () {
            Content.base.ensureId.call(this);

            // Synchronize the id of the builder if needed
            if (this.builder) { // This method might be called before the builder is created
                this.builder.addAttributes({
                    id: this.id
                });
            }
        },

        isContent: function () {
            return true;
        },

        setContent: function (content) {
            this.content = content;
        },

        getContent: function () {
            return this.content;
        },

        build: function () {
            this.builder.clear();

            if (this.content) {
                this.builder.addText(this.content);
            }

            return this.builder;
        },

        onRender: function () {

            this.rendered = true; // Flag it as rendered

            this.callHandlers('onRender'); // Call any handler registered for this event
        }
    });

    Xjs.registerType(Content);

    return Content;
});